﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StudentReport.Logic;
using System.IO;
using System.Reflection;

namespace StudentReport
{
    public partial class MainWindow : Form
    {
        public MainWindow()
        {
            InitializeComponent();

        }

        private void InitializeDisplay()
        {
            //fill combo boxes   
            for (int i = 1; i <= Convert.ToInt32(Properties.Settings.Default.NumberOfTerms); i++)
            {
                TestComboBox.Items.Add(("Term" + i.ToString()));
            }
            for (int i = 1; i <= Convert.ToInt32(Properties.Settings.Default.NumberOfUnits); i++)
            {
                TestComboBox.Items.Add(("Unit" + i.ToString()));
            }

            TestComboBox.SelectedIndex = 0;

        }

        private string filePath = String.Empty;

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void SelectFileButton_Click(object sender, EventArgs e)
        {
            try
            {
                string strFileName = String.Empty;
                //Present to the user.
                if (WorkBookSelectDialog.ShowDialog() == DialogResult.OK)
                    //strFileName = WorkBookSelectDialog.FileName;
                    strFileName = WorkBookSelectDialog.FileName;

                if (String.IsNullOrEmpty(strFileName))
                    return;//user didn't select a file
                else
                {
                    filePath = strFileName;
                    ExcelAccess xlAccess = new ExcelAccess(strFileName);
                    string[] info = xlAccess.GetClassInfo();

                    AcademicYearTextBox.Text = info[0];
                    ClassLabel.Text = info[1];
                    MediumLabel.Text = info[2];
                    // TermLabel.Text = info[3];
                    TeacherLabel.Text = info[4];
                    NoOfStudentsLabel.Text = info[5];
                    SectionLabel.Text = info[6];

                    InitializeDisplay();
                    EnableButtons();
                }
            }

            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void EnableButtons()
        {
            SearchButton.Enabled = true;
            SearchTextBox.Enabled = true;
            TestComboBox.Enabled = true;
            ClassResultButton.Enabled = true;
            EditButton.Enabled = true;
        }

        private void SearchButton_Click(object sender, EventArgs e)
        {
            if (SearchTextBox.Text.Equals("Student Search"))
                SearchTextBox.Text = String.Empty;

            try
            {
                ExcelAccess xlAccess = new ExcelAccess(filePath);
                DataTable resultTable = xlAccess.SearchStudent(SearchTextBox.Text);

                if (resultTable.Rows.Count > 0)
                {
                    SearchResultsGridView.DataSource = resultTable;
                    SearchResultsGridView.Visible = true;
                }
                else
                {
                    MessageBox.Show("No match found !", "Search Results");
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }

        }

        public void SelectClassResults()
        {
            try
            {
                ExcelAccess xlAccess = new ExcelAccess(filePath);
                DataTable resultTable = xlAccess.SelectClassResults(TestComboBox.Text);
                ClassGridView.DataSource = resultTable;

            }
            catch (Exception) { }
        }

        private void SearchResultClickAction(string admitionNo, string studentName)
        {
            SearchResultsGridView.Visible = false;
            StudentResults results = new StudentResults(filePath, admitionNo, studentName);
            results.Show();
        }

        private void SearchResultsGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string admitionNo = SearchResultsGridView[0, e.RowIndex].Value.ToString();
            string studentName = SearchResultsGridView[1, e.RowIndex].Value.ToString();
            SearchResultClickAction(admitionNo, studentName);
        }

        private void SearchResultsGridView_Leave(object sender, EventArgs e)
        {
            SearchResultsGridView.Visible = false;
        }

        private void MainWindow_MouseClick(object sender, MouseEventArgs e)
        {
            SearchResultsGridView.Visible = false;
        }

        private void SearchTextBox_MouseClick(object sender, MouseEventArgs e)
        {
            SearchBoxClick();
        }

        private void SearchBoxClick()
        {

            SearchTextBox.Text = String.Empty;
            SearchTextBox.ForeColor = Color.Black;

        }

        private void EditButton_Click(object sender, EventArgs e)
        {
            ExcelAccess xlAccess = new ExcelAccess(filePath);
            xlAccess.OpenExcel();
        }

        private void TestComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectClassResults();
        }

        private void SearchTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString().Equals("Return"))
                SearchButton_Click(null, null);
        }

        private void ClassResultButton_Click(object sender, EventArgs e)
        {
            ClassResults classResults = new ClassResults(filePath, ClassLabel.Text, TeacherLabel.Text);
            classResults.Show();
        }

    }
}
